package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.dubbo.utils.TimeLineService;
import com.tanhua.model.domain.MovementTimeLine;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class MomentApiImpl implements MovementApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TimeLineService timeLineService;

    // 发布动态
    @Override
    public void publish(Movement movement) {
        movement.setPid(idWorker.getNextId("movement"));
        movement.setCreated(System.currentTimeMillis());
        mongoTemplate.save(movement);
        // 查询当前用户的好友数据
        /**
         * 下方如果用户的好友太多如10万个好友就会阻塞主进程，将他改造到异步进行
         */
        timeLineService.saveTimeLine(movement.getUserId(), movement.getId());
//        Criteria criteria = Criteria.where("userId").is(movement.getUserId());
//        Query query = Query.query(criteria);
//        List<Friend> friends = mongoTemplate.find(query, Friend.class);
//        for (Friend friend : friends) {
//            MovementTimeLine timeLine = new MovementTimeLine();
//            timeLine.setMovementId(movement.getId());
//            timeLine.setUserId(friend.getUserId());
//            timeLine.setFriendId(friend.getFriendId());
//            timeLine.setCreated(System.currentTimeMillis());
//            mongoTemplate.save(timeLine);
//        }
    }

    @Override
    public PageResult findById(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria).skip((long) (page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Movement> movements = mongoTemplate.find(query, Movement.class);
        return new PageResult(page, pagesize, 0L, movements);
    }


    /**
     * 查询当前用户好友发布的所有动态
     *
     * @param page
     * @param pagesize
     * @param friendId
     * @return
     */
    @Override
    public List<Movement> findFriendMovements(Integer page, Integer pagesize, Long friendId) {
        Query query = Query.query(Criteria.where("friendId").is(friendId))
                .skip((page - 1) * pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<MovementTimeLine> lineList = mongoTemplate.find(query, MovementTimeLine.class);
        List<ObjectId> list = CollUtil.getFieldValues(lineList, "movementId", ObjectId.class);
        Query movementQuery = Query.query(Criteria.where("id").is(list));
        return mongoTemplate.find(movementQuery, Movement.class);
    }
}
